﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;
using HR.Appointments.Service.ValueObjects;

public partial class VacantSlots : UserPage
{
    #region Public Properties
    public DateTime BookingDate
    {
        get { return _calendar.SelectedDate; }
    }

    public TimeUnit BookingTime
    {
        get { return TimeUnit.Create(buttonList.SelectedValue); }
    }

    public int ActivityId
    {
        get { return Convert.ToInt32(Request.QueryString["ActivityId"]); }
    }

    public int WorkstationId
    {
        get { return Convert.ToInt32(Request.QueryString["WorkstationId"]); }
    }
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (IsLoggedIn)
            {
                _imgConfirm.Visible = true;
                _lbtnCompanyBookingSub.Visible = true;
                _lbtnCompany.Visible = false;
                _lblFindTime.Visible = true;
            }
            else
            {
                _imgConfirm.Visible = false;
                _lbtnCompanyBookingSub.Visible = false;
                _lblLogin.Visible = false;
                _lbtnCompanyBooking.Visible = false;
                _lbtnCompany.Visible = true;
                _lblFindTime.Visible = true;
            }
            _calendar.SelectedDate = DateTime.Today;
            ListVacantSlots(_calendar.SelectedDate);

            if (buttonList.Items.Count > 0)
            {
                buttonList.SelectedIndex = 0;
            }
            ListOpenHours();
        }
    }

    protected void _calendar_SelectedDateChanged(object source, DevExpress.Web.ASPxDataControls.SelectedDateChangedEventArgs e)
    {
        if (_calendar.SelectedDate.CompareTo(new DateTime(1900, 1, 1)) > 0)
        {
            if (_calendar.SelectedDate.CompareTo(DateTime.Today) < 0)
            {
                _lblNoFreeTime.Text = "Ekki hægt að bóka aftur í tímann";
                buttonList.Visible = false;
                return;

            }
            buttonList.Visible = true;
            ListVacantSlots(_calendar.SelectedDate);
        }
    }

    protected void btnBookOrder_Click(object sender, EventArgs e)
    {
        string activityId = Request.QueryString["ActivityId"];
        string workstationId = Request.QueryString["WorkstationId"];
        string time = buttonList.SelectedValue;
        if (!string.IsNullOrEmpty(activityId) &&
            !string.IsNullOrEmpty(workstationId) &&
            !string.IsNullOrEmpty(time))
        {
            Server.Transfer("~/UserInfo.aspx", true);
        }
    }
    #endregion

    #region Link Transfer
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx", true);
        }
	}

    protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Companybooking.aspx", true);
    }

    protected void _lbtnCompanyBookingSub_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Companybooking.aspx", true);
    }

    protected void _lbtnCompany_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Company.aspx", true);
    }
    #endregion

    #region Private List Methods
    private void ListVacantSlots(DateTime date)
    {

        string activityId = Request.QueryString["ActivityId"];
        string workstationId = Request.QueryString["WorkstationId"];
        if (!string.IsNullOrEmpty(activityId) && !string.IsNullOrEmpty(workstationId))
        {
            buttonList.DataSource = Facade.Anonymous().GetVacantSlots(
                GetCompanyIdFromCookie(), date,
                Convert.ToInt32(workstationId),
                Convert.ToInt32(activityId));
            buttonList.DataBind();
            if (buttonList.Items.Count == 0)
            {
                _lblNoFreeTime.Text = "Enginn laus tími í dag";
            }
            else
            {
                _lblNoFreeTime.Text = "";
            }
        }
        else
        {
            Redirect();
        }

    }
    
    private void ListOpenHours()
	{
		_dtgOpenHours.DataSource =
			Facade.Anonymous().GetOpenHours(GetCompanyIdFromCookie());
		_dtgOpenHours.DataBind();
    }
    #endregion
}
